﻿using UnityEngine;
using System.Collections.Generic;
using UnityEngine.Networking;

// [NetworkSettings(sendInterval = 0.05f)]
public class SnakeHead : NetworkBehaviour {

    public int bodyCount;
    public GameObject bodyPrefab;

    public List<SnakeBody> bodys = new List<SnakeBody>(5); 

    public override void OnStartLocalPlayer() {
        // all player create bodys
        CmdInitSnake();
    }

    [Command]
    public void CmdInitSnake() {
        var last = this.transform;
        for (var i = 0; i < bodyCount; i++) {
            var go = (GameObject)Instantiate(bodyPrefab, this.transform.position, Quaternion.identity);
            var body = go.GetComponent<SnakeBody>();
            body.prevBody = last.transform;
            // add to list
            bodys.Add(body);
            last = go.transform;
            // Spawn to client
            NetworkServer.Spawn(go);
        }
    }

    [Command]
    public void CmdChargeAll()
    {
        foreach (var snakeBody in bodys)
        {
            var v = snakeBody.GetComponent<VelocitySimulate>();
            v.addVelocity(v.LastVelocity.normalized * 5f, 0.5f);
        } 
    }

}
